﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmxetnghiem : Form
    {
        public string _xnTenBN;
        public static string _TenBN;
        public static string _tenloai;
        public static string _maloai;
        public static DataTable _dtXN = new DataTable();
        public static long _sum = 0;

        public frmxetnghiem()
        {
            InitializeComponent();

            TV_Tonghop.Themcot("MA_LOAIXN", "Mã Loại Xét Nghiệm", dt_loaixetnghiem);
            TV_Tonghop.Themcot("TEN_LOAIXN", "Tên Loại Xét Nghiệm", dt_loaixetnghiem);

            TV_Tonghop.Themcot("MA_LOAIXN", "Loại Xét Nghiệm", dt_loaixetnghiem);
            TV_Tonghop.Themcot("ngay_thang_nam", "Ngày Áp Dụng", dt_loaixetnghiem);
            TV_Tonghop.Themcot("DON_GIA_XET_NGHIEM", "Đơn Giá", dt_loaixetnghiem);

        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

        private void frmxetnghiem_Load(object sender, EventArgs e)
        {
            DataTable dtb = AddID.Ham_Tu_Tang.Select_Max("LOAI_XET_NGHIEM_MAU", "Ma_LoaiXN");
            string chuoi = dtb.Rows[0]["id"] + "";
            string kq = AddID.Ham_Tu_Tang.AddID(chuoi, 2, 2);

            cb_maloaixetnghiem.Text = kq;


            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienThiLoaiXetNghiem", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            SqlDataAdapter ad1 = new SqlDataAdapter("HienThiDonGiaXetNghiem", ketnoi);
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            ad1.Fill(dt1);
            ad.Fill(dt);
            dt_dongiaxetnghiem.DataSource = dt1;
            dt_loaixetnghiem.DataSource = dt;// do du lieu vao DataGridView


            ketnoi.Open();
            SqlCommand cmd1 = new SqlCommand(@"select * from Loai_Xet_Nghiem_Mau", ketnoi); // lấy dữ liệu
            SqlCommand cmd2 = new SqlCommand(@"select count(Ma_LoaiXN) as dem from Loai_Xet_Nghiem_Mau", ketnoi); // lấy số dòng
            DataTable dt2 = new DataTable();
            DataTable dt3 = new DataTable();
            dt2.Load(cmd1.ExecuteReader());
            dt3.Load(cmd2.ExecuteReader());

            int so = (int)dt3.Rows[0]["dem"];
            for (int i = 0; i < so; i++ )
                ckListHuyetdo.Items.Add(dt2.Rows[i]["Ten_LoaiXN"]);

         

            dtm_ngayapdung.Text = Convert.ToString(DateTime.Now);

        }

        private void bt_Them_Click(object sender, EventArgs e)
        {
            

            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemLoaiXetNghiemMau", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai_xn", SqlDbType.NChar).Value = cb_maloaixetnghiem.Text;
            cmd.Parameters.Add("@ten_loai_xn", SqlDbType.NVarChar).Value = tb_tenxetnghiem.Text;
            cmd.Parameters.Add("@don_gia", SqlDbType.BigInt).Value = Convert.ToInt64(txb_dongia.Text);
            cmd.Parameters.Add("@ngay_ap_dung", SqlDbType.DateTime).Value =  DateTime.Now;
            

            cmd.ExecuteNonQuery();
            frmxetnghiem_Load(sender, e);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatXetNghiem", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai_xn", SqlDbType.NChar).Value = cb_maloaixetnghiem.Text;
            cmd.Parameters.Add("@ten_loai_xn", SqlDbType.NVarChar).Value = tb_tenxetnghiem.Text;
            cmd.Parameters.Add("@don_gia", SqlDbType.BigInt).Value = txb_dongia.Text;
            cmd.Parameters.Add("@ngay_xn", SqlDbType.DateTime).Value = dat_ngayapdung.Text;

            cmd.ExecuteNonQuery();
            frmxetnghiem_Load(sender, e);
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaLoaiXetNghiemMau", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai_xn", SqlDbType.NChar).Value = cb_maloaixetnghiem.Text;


            cmd.ExecuteNonQuery();
            frmxetnghiem_Load(sender, e);
        }

        int dong;

        private void dt_loaixetnghiem_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            cb_maloaixetnghiem.Text = dt_loaixetnghiem.Rows[dong].Cells["MA_LOAIXN"].Value.ToString();
        
            tb_tenxetnghiem.Text = dt_loaixetnghiem.Rows[dong].Cells["TEN_LOAIXN"].Value.ToString();

            _tenloai = tb_tenxetnghiem.Text;
            _maloai = cb_maloaixetnghiem.Text;
        }
                
        private void stb_dongiaxetnghiemmau_Click(object sender, EventArgs e)
        {
            
            txtTenLoaiXN.Text = _tenloai;
            
        }

        private void tpXetNghiemHuyetHoc_Click(object sender, EventArgs e)
        {
            
            //DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            
        }

        private void bt_ThemDonGia_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemDonGiaXetNghiemMau", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai", SqlDbType.NChar).Value = _maloai;
            cmd.Parameters.Add("@ngay_ap_dung", SqlDbType.DateTime).Value = Convert.ToDateTime( dtm_ngayapdung.Value);
            cmd.Parameters.Add("@don_gia_xn", SqlDbType.BigInt).Value = tb_dongia.Text;
            cmd.ExecuteNonQuery();
            frmxetnghiem_Load(sender, e);
        }

        private void bt_SuaDonGia_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatDonGiaXetNghiem", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai", SqlDbType.NChar).Value = txtTenLoaiXN.Text;
            cmd.Parameters.Add("@ngay_ap_dung", SqlDbType.DateTime).Value = dtm_ngayapdung.Text;
            cmd.Parameters.Add("@don_gia_xn", SqlDbType.BigInt).Value = tb_dongia.Text;

            cmd.ExecuteNonQuery();
            frmxetnghiem_Load(sender, e);
        }

        private void bt_XoaDonGia_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaDonGiaXetNghiemMau", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_loai", SqlDbType.NChar).Value = txtTenLoaiXN.Text;


            cmd.ExecuteNonQuery();
            frmxetnghiem_Load(sender, e);
        }

        private void dt_dongiaxetnghiem_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            string _mabn = frmkhambenh._ma;
            string _chondv = "";
            int n = ckListHuyetdo.CheckedItems.Count;
            for (int i = 0; i < n ; i++)
            {
                _chondv += "N'"+ckListHuyetdo.CheckedItems[i] +"'";
                if (i < n - 1)
                    _chondv += ", ";

            }

           // SqlConnection ketnoi = getconnect();
           // ketnoi.Open();
           // SqlCommand cmd = new SqlCommand(@"  SELECT * FROM LOAI_XET_NGHIEM_MAU WHERE TEN_LOAIXN IN (" + _chondv + ")", ketnoi);
            
           //// var TB = new DataTable();
           //_dtXN.Load(cmd.ExecuteReader());

           //_sum = Convert.ToInt64(_dtXN.Compute("sum(don_gia)", ""));

            _TenBN = _xnTenBN;
            frmkhambenh frm = new frmkhambenh();
            frm.chon = _chondv;
            frm._xntruyentenbn = _TenBN;
            this.Close();
            frm.Show();
            
            
            
        }

        private void dat_ngayapdung_Click(object sender, EventArgs e)
        {

        }

        

       
    }
}
